//自定义登录模块
//负责检查用户名密码。在parse使用LinkWith时用于鉴权

// Don't require or import parse-server in this module.
// See this issue: https://github.com/parse-community/parse-server/issues/6467

const Parse = require('parse/node');
const UserObj = Parse.Object.extend("v5_user");


//检查用户名密码是否匹配
//authData={id,access_token}
async function validateAuthData(authData, options) {
    const {id:unionid,access_token}=authData;
    //查找表中是否已经有用户
    const query = new Parse.Query(UserObj);
    query.notEqualTo('isDeleted', true);
    query.equalTo('unionid', unionid);
    query.equalTo('access_token', access_token);
    const authenticatedParseUser= await query.first({ useMasterKey: true });
    if(!authenticatedParseUser){
        throw "用户不存在或密码错误";
    }else{
        return authenticatedParseUser.toJSON();
    }
}

function validateAppId(appIds, authData, options) {
    return Promise.resolve({});
}

module.exports = {
    validateAppId,
    validateAuthData,
};